package com.jlf.starter.redis.reactive.context;

import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.NonNull;

/**
 * 默认的缓存序列化的上下文
 *
 * @author wujr
 * 2023/8/4
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/8/4 1.0 新增]
 */
public class DefaultRedisSerializationContext implements RedisSerializationContext<String, Object> {
    private final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    private final GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

    @Override
    @NonNull
    public SerializationPair<String> getKeySerializationPair() {
        return SerializationPair.fromSerializer(stringRedisSerializer);
    }

    @Override
    @NonNull
    public SerializationPair<Object> getValueSerializationPair() {
        return SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer);
    }

    @Override
    @NonNull
    @SuppressWarnings("unchecked")
    public <HK> SerializationPair<HK> getHashKeySerializationPair() {
        return (SerializationPair<HK>)SerializationPair.fromSerializer(stringRedisSerializer);
    }

    @Override
    @NonNull
    @SuppressWarnings("unchecked")
    public <HV> SerializationPair<HV> getHashValueSerializationPair() {
        return (SerializationPair<HV>)SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer);
    }

    @Override
    @NonNull
    public SerializationPair<String> getStringSerializationPair() {
        return SerializationPair.fromSerializer(stringRedisSerializer);
    }
}
